﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Models;
using StarryEdge.Core.Parameter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Menu.User.ViewModels.Dialog
{
    public class AddUserPopupDialog : Prism.Mvvm.BindableBase
    {
        public ObservableCollection<RoleModel> Roles { get; set; }
        public UserModel User { get; set; }
        private IUserEntity _userEentity;
        public AddUserPopupDialog(IUserEntity userEentity, UserModel userModel)
        {
            _userEentity = userEentity;
            Roles = new ObservableCollection<RoleModel>();
            foreach (var item in userEentity.GetRoles())
            {
                Roles.Add(item);
            }
            if (userModel == null)
            {
                User = new UserModel()
                {
                    Id = Guid.NewGuid(),
                    RegisterTime = DateTime.Now,
                    FrameTime = $"00:00-00:00"
                };
                _startTime = "00:00";
                _endTime = "00:00";
            }
            else
            {
                User = userModel;
            }
            if (userModel != null && !string.IsNullOrEmpty(userModel.FrameTime) && userModel.FrameTime.Contains("-"))
            {
                string[] strings = userModel.FrameTime.Split('-');
                if (strings.Length == 2)
                {
                    _startTime = strings[0];
                    _endTime = strings[1];
                }
            }
            else
            {
                _startTime = "00:00";
                _endTime = "00:00";
            }
        }

        private string _startTime;

        public string StartTime
        {
            get { return _startTime; }
            set { 
                SetProperty(ref _startTime, value);
                User.FrameTime = $"{value}-{_endTime}";
            }
        }

        private string _endTime;

        public string EndTime
        {
            get { return _endTime; }
            set { 
                SetProperty(ref _endTime, value);
                User.FrameTime = $"{_startTime}-{value}";
            }
        }

        public DelegateCommand Save => new DelegateCommand(() =>
        {
            _userEentity.AddUser(User);
            _userEentity.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString()))
            {
                DialogHost.Close(PopUpTypes.Global.ToString());
            }
        });
    }
}
